//
//  AppendDetailsTableViewCell.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/6.
//

#import "AppendDetailsTableViewCell.h"

@interface AppendDetailsTableViewCell ()
@property (nonatomic ,strong) UILabel * tissue;
@property (nonatomic ,strong) UILabel * breedCycle;
@property (nonatomic ,strong) UILabel * director;
@property (nonatomic ,strong) UILabel * templab1;
@property (nonatomic ,strong) UILabel * templab2;

@property (nonatomic ,strong) UILabel * area;
@property (nonatomic ,strong) UIImageView * onephoto;
@property (nonatomic ,strong) UIImageView * twephoto;
@property (nonatomic ,strong) UIImageView * threephoto;


@end

@implementation AppendDetailsTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.contentView.clipsToBounds = YES;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UIView * bkview = [UIView new];
//    bkview.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bkview];
    [bkview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(8));
        make.right.offset(kwt(-8));
        make.top.bottom.offset(0);
    }];
    UILabel * temp5 = [DTKTool labelWithText:@"已经到底了～" andFont:kFont(14) andTextColor:KcolorHex(@"#787A79")];
    [bkview addSubview:temp5];
    [temp5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(kwt(12));
        make.bottom.offset(kwt(-15));
        make.height.offset(kwt(20));
    }];
    UIView * backview = [UIView new];
    backview.backgroundColor = UIColor.whiteColor;
    [bkview addSubview:backview];
    [DTKTool YJ:kwt(7) andView:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
 
        make.top.left.right.offset(0);
        make.bottom.offset(kwt(-50));
    }];
    
    UIView * leftline = [UIView new];
    leftline.backgroundColor = KcolorHex(@"#0967FF");
    [DTKTool BFYJ:kwt(2) andView:leftline andCorner:UIRectCornerTopRight|UIRectCornerBottomRight andSize:CGRectMake(0, 0, kwt(4), kwt(15))];
    [backview addSubview:leftline];
    [leftline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.offset(kwt(16));
        make.height.offset(kwt(15));
        make.width.offset(kwt(4));
    }];
    
    UILabel * temp = [DTKTool labelWithText:@"池塘信息" andFont:kCFont(19) andTextColor:KcolorHex(@"#222222")];
    [backview addSubview:temp];
    [temp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(14));
    }];
    
    UILabel * templab = [DTKTool labelWithText:@"池塘编组" andFont:kFont(17) andTextColor:KcolorHex(@"#787A79")];
    [backview addSubview:templab];
    [templab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(46));
    }];
    
    UILabel * tissue = [DTKTool labelWithText:@"--" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    self.tissue = tissue;
    tissue.numberOfLines = 0;
    [backview addSubview:tissue];
    [tissue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(100));
        make.width.offset(kwt(250));
        make.top.equalTo(templab.mas_top);
    }];
    
    
    UILabel * templab1 = [DTKTool labelWithText:@"养殖周期" andFont:kFont(17) andTextColor:KcolorHex(@"#787A79")];
    self.templab1 = templab1;
    [backview addSubview:templab1];
    [templab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.equalTo(tissue.mas_bottom).offset(kwt(15));
    }];
    
    UILabel * breedCycle = [DTKTool labelWithText:@"--" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    self.breedCycle = breedCycle;
    [backview addSubview:breedCycle];
    [breedCycle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(100));
        make.width.offset(kwt(250));
        make.top.equalTo(templab1.mas_top);
    }];
    
    UILabel * templab2 = [DTKTool labelWithText:@"负责人" andFont:kFont(17) andTextColor:KcolorHex(@"#787A79")];
    self.templab2 = templab2;
    [backview addSubview:templab2];
    [templab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.equalTo(breedCycle.mas_bottom).offset(kwt(15));
        make.height.offset(kwt(17));
    }];
    
    UILabel * director = [DTKTool labelWithText:@"--" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    self.director = director;
    [backview addSubview:director];
    [director mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(100));
        make.width.offset(kwt(250));
        make.top.equalTo(templab2.mas_top);
    }];
    UILabel * templab3 = [DTKTool labelWithText:@"池塘面积" andFont:kFont(17) andTextColor:KcolorHex(@"#787A79")];
    [backview addSubview:templab3];
    [templab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.equalTo(director.mas_bottom).offset(kwt(15));
    }];
    
    UILabel * area = [DTKTool labelWithText:@"--" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    self.area = area;
    [backview addSubview:area];
    [area mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(100));
        make.width.offset(kwt(250));
        make.top.equalTo(templab3.mas_top);
    }];
    
    UILabel * templab4 = [DTKTool labelWithText:@"池塘照片" andFont:kFont(17) andTextColor:KcolorHex(@"#787A79")];
    [backview addSubview:templab4];
    [templab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.equalTo(area.mas_bottom).offset(kwt(15));
    }];
    MJWeakSelf
    UIImageView * onephoto = [UIImageView new];
    self.onephoto = onephoto;
    
    [DTKTool YJ:kwt(2) andView:onephoto];
    [backview addSubview:onephoto];
    [onephoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(100));
        make.top.equalTo(templab4.mas_top);
        make.width.height.offset(kwt(80));
    }];
    UIImageView * twephoto = [UIImageView new];
    self.twephoto = twephoto;
    [DTKTool YJ:kwt(2) andView:twephoto];
    [backview addSubview:twephoto];
    [twephoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(onephoto.mas_right).offset(kwt(5));
        make.top.equalTo(templab4.mas_top);
        make.width.height.offset(kwt(80));
    }];
    
    UIImageView * threephoto = [UIImageView new];
    self.threephoto = threephoto;
    [DTKTool YJ:kwt(2) andView:threephoto];
    [backview addSubview:threephoto];
    [threephoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(twephoto.mas_right).offset(kwt(5));
        make.top.equalTo(templab4.mas_top);
        make.width.height.offset(kwt(80));
    }];
    
    onephoto.addClickBlock(^{
        [weakSelf onephotoClick];
    });
    twephoto.addClickBlock(^{
        [weakSelf twephotoClick];
    });
    threephoto.addClickBlock(^{
        [weakSelf threephotoClick];
    });
}

- (void)setModel:(AppendDetailsModel *)model{
    _model = model;
    self.tissue.text = model.tissue;

    if(model.typeCode == 1){///养殖周期
        self.breedCycle.text = [NSString stringWithFormat:@"%@天",model.breedCycle];
        if(model.postponeDays > 0){
            NSMutableAttributedString * str = [DTKTool AttributedStringWithStr:  [NSString stringWithFormat:@"%@天",model.breedCycle] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#222222") andFont:kCFont(17) andPattenr:@""];
            NSMutableAttributedString * yqstr = [DTKTool AttributedStringWithStr:  [NSString stringWithFormat:@" (延期 %ld天)",model.postponeDays] andNodeColor:KcolorHex(@"#FF942B") andSeledColor:KcolorHex(@"#FF942B") andFont:kCFont(17) andPattenr:@""];
            [str appendAttributedString:yqstr];
            self.breedCycle.attributedText = str;
        }
        
        self.templab1.hidden = NO;
        [self.templab2 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kwt(12));
            make.height.offset(kwt(17));
            make.top.equalTo(self.breedCycle.mas_bottom).offset(kwt(15));
        }];
    } else {
        self.breedCycle.text = @"";
        self.templab1.hidden = YES;
        [self.templab2 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kwt(12));
            make.height.offset(kwt(17));
            make.top.equalTo(self.tissue.mas_bottom).offset(kwt(15));
        }];
    }
    ///负责人
    self.director.text = [NSString stringWithFormat:@"%@ (%@)",model.directorName,model.directorPhone];

    self.area.text = [NSString stringWithFormat:@"%@亩", model.area];

    if(model.pictureImg.length > 0){
        NSArray * imgarray = [model.pictureImg componentsSeparatedByString:@","];
        if(imgarray.count > 0){
            self.onephoto.hidden = NO;
            [self.onephoto LZsetImageWithImagePath:imgarray.firstObject placeholderImage:[UIImage imageNamed:@"tangkou_photo"]];
        } else {
            self.onephoto.hidden = YES;
        }
        if(imgarray.count > 1){
            self.twephoto.hidden = NO;
            [self.twephoto LZsetImageWithImagePath:imgarray[1] placeholderImage:[UIImage imageNamed:@"tangkou_photo"]];
        } else {
            self.twephoto.hidden = YES;
        }
        if(imgarray.count > 2){
            self.threephoto.hidden = NO;
            [self.threephoto LZsetImageWithImagePath:imgarray[2] placeholderImage:[UIImage imageNamed:@"tangkou_photo"]];
        } else {
            self.threephoto.hidden = YES;
        }
    } else {
        self.onephoto.hidden = self.twephoto.hidden = self.threephoto.hidden = YES;
    }
}

- (void)onephotoClick{
    [DTKTool openImagAndVideo:[self.model.pictureImg componentsSeparatedByString:@","] andopenview:self.onephoto andIndex:0];
}

- (void)twephotoClick{
    [DTKTool openImagAndVideo:[self.model.pictureImg componentsSeparatedByString:@","] andopenview:self.twephoto andIndex:1];

}

- (void)threephotoClick{
    [DTKTool openImagAndVideo:[self.model.pictureImg componentsSeparatedByString:@","] andopenview:self.threephoto andIndex:2];

}

@end
